﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Class_Entity;
using BussinessObject;

public partial class WebApplication_ADEncapsulation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        

        if (!Page.IsPostBack)
        {
            loadGrid();
           
        }
        

    }
    //direct to add new page
    protected void btNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("ADAddEncapsulation.aspx");
    }
    

    private void loadGrid()
    {
        Encapsulation en = new Encapsulation();
        BOProducts pro = new BOProducts();

        //load data to gridview
        GridView1.DataSource = pro.Load_Encapsulation(en);
        GridView1.DataBind();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Encapsulation en = new Encapsulation();
        BOProducts pro = new BOProducts();

        //load information to object en
        int id = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);

        en.ID = id;
        //delete object en in database
        pro.Delete_Encapsulation(en);
        HttpContext.Current.Response.Write("<script type='text/javascript'>alert('" + "Delete Successfully ..." + "');</script>");
        loadGrid();
    }
    //direct to update page
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["ID"] = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);
        Response.Redirect("ADEditEncapsulation.aspx");
    }

    // search model by name
    protected void btSearch_Click(object sender, EventArgs e)
    {
        Encapsulation en = new Encapsulation();
        BOProducts pro = new BOProducts();
        
        en.Model = txtSearch.Text;

        GridView1.DataSource = pro.Search_Encapsulation(en);
        GridView1.DataBind();
    }
    
    // delete many object in one click
    protected void btDelete_Click(object sender, EventArgs e)
    {
        Encapsulation en = new Encapsulation();
        BOProducts pro = new BOProducts();

        // check every row if check box is checked or not then delete                     
            foreach (GridViewRow row in GridView1.Rows)
            {
                
                CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
                if (cb != null && cb.Checked)
                {

                    int id = Convert.ToInt32(row.Cells[1].Text);
                    en.ID = id;
                    pro.Delete_Encapsulation(en);
                    HttpContext.Current.Response.Write("<script type='text/javascript'>alert('" + "Delete Successfully ..." + "');</script>");
                }
            }

        loadGrid();

    }
}